package com.wechat.DAO.DaoImpl;

import com.wechat.DAO.UserDAO;
import com.wechat.pojo.User;

import java.sql.*;

public class UserDAOIpml implements UserDAO {
    Connection connection = null;
    PreparedStatement pst = null;
    ResultSet resultSet = null;
    /* （前半部分在servlet判断，后半段DAO
    * 增：查 => if不重复 => |user => Boolean
    * 删: 查 => if存在 => | Uid => Boolean
    * 改: 查 => if存在 => | user => Boolean
    * 查： uid => User
    * SQL 查询（用于复杂查询） sql => User
    * */
    @Override
    public String add(User user) {
        try {
            getConn();
            pst = connection.prepareStatement("insert into Users values (?,?,?,?,?)");
            pst.setInt(1,user.getUid());
            pst.setString(2,user.getNickname());
            pst.setInt(3,user.getSpeaks());
            pst.setInt(4,user.getHonor());
            pst.setInt(5,user.getLockDays());
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }finally {
            closeAll();
        }
        System.out.println(user.getUid()+"注册成功！");
        return "success";
    }

    @Override
    public String lock(int Uid, int lockDays) {
        try {
            getConn();
            pst = connection.prepareStatement("UPDATE USERS SET lockDays=? WHERE uid=?");
            pst.setInt(1,lockDays);
            pst.setInt(1,Uid);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }finally {
            closeAll();
        }
        System.out.println(Uid+"封号"+lockDays+"天成功");
        return "success";
    }

    @Override
    public String unlock(int Uid) {
        try {
            getConn();
            pst = connection.prepareStatement("UPDATE USERS SET lockDays=0 WHERE uid=?");
            pst.setInt(1,Uid);
            pst.execute();
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }finally {
            closeAll();
        }
        System.out.println(Uid+"成功解封");
        return "success";
    }

    @Override
    public String update(User user) {
            try {
                getConn();
                PreparedStatement pst = connection.prepareStatement("UPDATE USERS SET nickname=?,speaks=?,honor=? WHERE uid=?");
                pst.setString(1,user.getNickname());
                pst.setInt(2,user.getSpeaks());
                pst.setInt(3,user.getHonor());
                pst.setInt(4,user.getUid());
                pst.execute();
            } catch (Exception e) {
                e.printStackTrace();
                return "fail";
            }finally {
                closeAll();
            }
        System.out.println("用户"+user.getUid()+"资料更新成功！");
            return "success";
    }

    @Override
    public User query(int Uid) {
        User user = null;
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement("SELECT * FROM USERS WHERE uid=?");
            pst.setInt(1,Uid);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                user = new User(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getInt(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5)
                );
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            closeAll();
        }
        System.out.println("找到用户"+user.getUid());
        return user;
    }

    @Override
    public User querySQL(String sql) {
        User user = null;
        try {
            getConn();
            PreparedStatement pst = connection.prepareStatement(sql);
            resultSet = pst.executeQuery();
            while (resultSet.next()){
                user = new User(
                        resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getInt(3),
                        resultSet.getInt(4),
                        resultSet.getInt(5)
                );
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            closeAll();
        }
        System.out.println("找到用户"+user.getUid());
        return user;
    }

    private void getConn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql:///wechat?useSSL=false", "root", "123456");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void closeAll() {
        if(pst!=null){
            try {
                pst.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
